package kmp;

public class ViolenceMarch {
    public static void main(String[] args) {
        String str1 = "靳傻逼云逼龙靳云龙牛子";
        String str2 = "靳云龙~";
        System.out.println(violenceMarch(str1, str2));

    }

    /**
     * 暴力匹配
     *
     * @param str1 主串
     * @param str2 标注匹配串
     * @return
     */
    public static int violenceMarch(String str1, String str2) {
        char[] str1Char = str1.toCharArray();
        char[] str2Char = str2.toCharArray();

        //获得两个字符串的长度
        int str1Length = str1Char.length;
        int str2Length = str2Char.length;

        int i = 0; //主串的索引
        int j = 0; //匹配串的索引

        while (i < str1Length && j < str2Length) {
            if (str1Char[i] == str2Char[j]) {
                i++;
                j++;
            } else {
                i = i - j + 1;
                j = 0;
            }
        }
        if (j == str2Length) {
            return i - j;
        } else {
            return -1;
        }
    }
}
